/*
 * 作者：姚俊
 * 功能：专利报表
 * 日期：2015-05-24
 */
package teacher.service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.struts2.ServletActionContext;
import teacher.bean.Patent;
import teacher.bean.Teacher;
import teacher.service.patent;
import tools.StringDate;


public class PullPatentToExcel {
	
	/**
	 * @category 生成专利的报表
	 * @author yaojun
	 * @param flag int 值为0查询全部、1按照起止日期查询、2按照获奖名称模糊查询
	 * @param patent1 Patent 传入一个专利的对象，当查询全部的时候该对象为空，当按照起止日期查询的时候，该对象必须有日期
	 * 当进行名称模糊查询的时候该对象中必须有名称
	 * @param patent2  当根据日期查询的时候，该对象传入一个截止日期
	 */
	public static void patentExcel(int flag,Patent patent1,Patent patent2,Teacher t)
	{
        //专利列表
        List<Patent> patents=null;
        try {
            WritableWorkbook wwb = null; 
            String uploadPath = ServletActionContext.getServletContext()
					.getRealPath("/upload");
            
            File dir=new File(uploadPath);  

	        //如果这个目录不存在,则创建它  
	        if (!dir.exists()) {  
	            dir.mkdir();  
	        } 
               // 创建可写入的Excel工作簿
              String fileName = uploadPath+"/"+"patent.xls";
//            String fileName="D://patents.xls";
               File file=new File(fileName);
               if (!file.exists()) {
                   file.createNewFile();
               }
               //以fileName为文件名来创建一个Workbook
               wwb = Workbook.createWorkbook(file);
               // 创建工作表
               WritableSheet ws = wwb.createSheet("Test Shee 1", 0);   
               
               
               if(flag==0)
               {
            	   patents=new patent().selectPatentByTeacher(t);
               }
               else if(flag==1)
               {
            	   
            	   patents=new patent().selectTimePatentTea(t,
            			   patent1.getEmpowerDate(),patent2.getEmpowerDate());
               }
               else
               {
            	   patents=new patent().selectVaguePatentTea(patent1.getName(), t);
               }
               
               
               
               //要插入到的Excel表格的行号，默认从0开始
               Label pid= new Label(0, 0, "编号");//表示第
               Label pname= new Label(1, 0, "专利名称");//表示第
               Label pfiestInventor= new Label(2, 0, "第一发明人");
               Label pempowerDate= new Label(3, 0, "授权日期");   
               Label pempowerUnit= new Label(4, 0, "专利申请号");
               Label ptransferFunds= new Label(5, 0, "专利类别");
   
               
        
               ws.addCell(pid);
               ws.addCell(pname);
               ws.addCell(pfiestInventor);
               ws.addCell(pempowerDate);
               ws.addCell(pempowerUnit);
               ws.addCell(ptransferFunds);

               

               for (int i = 0; i < patents.size(); i++) {  
            	   String ee=null;
            	   Label ppid= new Label(0, i+1, i+1+"");
                   Label ppname= new Label(1, i+1, patents.get(i).getName()+"");
                   Label ppfiestInventor= new Label(2, i+1, patents.get(i).getFiestInventor());
                   Label ppempowerDate= new Label(3, i+1,
                		   StringDate.DateToString(
                				   new SimpleDateFormat("yyyy-MM-dd").parse(
                						   StringDate.DateToString(
                								   patents.get(i).getEmpowerDate())))); 
                   Label ppempowerUnit= new Label(4, i+1, patents.get(i).getApplyNumber()+"");  
//                 Label pptransferFunds= new Label(5, i+1, patents.get(i).getTransferFunds()+"");  
                   if(patents.get(i).getExpClass()==0){
                	  ee="发明专利";
                   }else if(patents.get(i).getExpClass()==1){
                	   ee="实用新型专利";
                   }else{
                	   ee="软件著作权";  
                   }
                   Label pptransferFunds= new Label(5, i+1, ee+""); 
    
                   
                   
                   ws.addCell(ppid);
                   ws.addCell(ppname);
                   ws.addCell(ppfiestInventor);
                   ws.addCell(ppempowerDate);          
                   ws.addCell(ppempowerUnit);
                   ws.addCell(pptransferFunds);
               }  
              //写进文档
               wwb.write();
              // 关闭Excel工作簿对象
               wwb.close();     
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
	
	public static void patentExcel2(List<Patent> patents)
	{
        //专利列表
        try {
            WritableWorkbook wwb = null; 
            String uploadPath = ServletActionContext.getServletContext()
					.getRealPath("/upload");
            
            File dir=new File(uploadPath);  

	        //如果这个目录不存在,则创建它  
	        if (!dir.exists()) {  
	            dir.mkdir();  
	        } 
               // 创建可写入的Excel工作簿
              String fileName = uploadPath+"/"+"patent.xls";
//            String fileName="D://patents.xls";
               File file=new File(fileName);
               if (!file.exists()) {
                   file.createNewFile();
               }
               //以fileName为文件名来创建一个Workbook
               wwb = Workbook.createWorkbook(file);
               // 创建工作表
               WritableSheet ws = wwb.createSheet("Test Shee 1", 0);   
               
               
             
               //要插入到的Excel表格的行号，默认从0开始
               Label pid= new Label(0, 0, "编号");//表示第
               Label pname= new Label(1, 0, "专利名称");//表示第
               Label pfiestInventor= new Label(2, 0, "第一发明人");
               Label pempowerDate= new Label(3, 0, "授权日期");   
               Label pempowerUnit= new Label(4, 0, "专利申请号");
               Label ptransferFunds= new Label(5, 0, "专利类别");
   
               
        
               ws.addCell(pid);
               ws.addCell(pname);
               ws.addCell(pfiestInventor);
               ws.addCell(pempowerDate);
               ws.addCell(pempowerUnit);
               ws.addCell(ptransferFunds);

               

               for (int i = 0; i < patents.size(); i++) {  
            	   String ee=null;
            	   Label ppid= new Label(0, i+1, i+1+"");
                   Label ppname= new Label(1, i+1, patents.get(i).getName()+"");
                   Label ppfiestInventor= new Label(2, i+1, patents.get(i).getFiestInventor());
                   Label ppempowerDate= new Label(3, i+1,
                		   StringDate.DateToString(
                				   new SimpleDateFormat("yyyy-MM-dd").parse(
                						   StringDate.DateToString(
                								   patents.get(i).getEmpowerDate())))); 
                   Label ppempowerUnit= new Label(4, i+1, patents.get(i).getApplyNumber()+"");  
//                 Label pptransferFunds= new Label(5, i+1, patents.get(i).getTransferFunds()+"");  
                   if(patents.get(i).getExpClass()==0){
                	  ee="发明专利";
                   }else if(patents.get(i).getExpClass()==1){
                	   ee="实用新型专利";
                   }else{
                	   ee="软件著作权";  
                   }
                   Label pptransferFunds= new Label(5, i+1, ee+""); 
    
                   
                   
                   ws.addCell(ppid);
                   ws.addCell(ppname);
                   ws.addCell(ppfiestInventor);
                   ws.addCell(ppempowerDate);          
                   ws.addCell(ppempowerUnit);
                   ws.addCell(pptransferFunds);
               }  
              //写进文档
               wwb.write();
              // 关闭Excel工作簿对象
               wwb.close();     
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }

}
